<template>
  <el-dialog v-model="visible" :title="!dataForm.supplierId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="供应商名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactName">
        <el-input v-model="dataForm.contactName" placeholder="联系人姓名"></el-input>
      </el-form-item>
      <el-form-item label="供应商电子邮件" prop="email">
        <el-input v-model="dataForm.email" placeholder="供应商电子邮件"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="供应商地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="供应商地址"></el-input>
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input v-model="dataForm.city" placeholder="城市"></el-input>
      </el-form-item>
      <el-form-item label="州/省" prop="state">
        <el-input v-model="dataForm.state" placeholder="州/省"></el-input>
      </el-form-item>
      <el-form-item label="邮政编码" prop="postalCode">
        <el-input v-model="dataForm.postalCode" placeholder="邮政编码"></el-input>
      </el-form-item>
      <el-form-item label="国家" prop="country">
        <el-input v-model="dataForm.country" placeholder="国家"></el-input>
      </el-form-item>
      <el-form-item label="信用额度" prop="creditLimit">
        <el-input v-model="dataForm.creditLimit" placeholder="信用额度"></el-input>
      </el-form-item>
      <el-form-item label="供应商状态" prop="status">
        <el-input v-model="dataForm.status" placeholder="供应商状态"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  supplierId: "",
  name: "",
  contactName: "",
  email: "",
  phone: "",
  address: "",
  city: "",
  state: "",
  postalCode: "",
  country: "",
  creditLimit: "",
  status: "",
  createdAt: "",
  updatedAt: "",
});

const rules = ref({
});

const init = (supplierId?: number) => {
  visible.value = true;
  dataForm.supplierId = supplierId;

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (supplierId) {
    getInfo(supplierId);
  }
};

// 获取信息
const getInfo = (supplierId: number) => {
  baseService.get("/sys/suppliers/" + supplierId).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.supplierId ? baseService.post : baseService.put)("/sys/suppliers", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>